/**
 * 首页脚本
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    var util = require("util");
    require('authorize');
    require("permission");

    $.get('/api/index.php?r=/common/options/get-index-root-path', function (res) {
        if (res.retCode == 0) {
            // 显示产品环境页面
            var url = '';
            if (util.getUrlPara('type') === 'project') {
                url = res.data + '/admin/home/project/index';
            } else {
                url = res.data + '/admin/home/index/index';
            }

            var $pageIframeWrapper = $("#page_iframe_wrapper");

            function getIframeHeight() {
                return $(window).height() - $('body>.top').outerHeight() - $('body>.footer').outerHeight() - 55;
            }

            var $ifram = $('<iframe frameborder="0" style="width:100%;"></iframe>')
                .attr('src', url)
                .height(getIframeHeight())
                .appendTo($pageIframeWrapper);

            $(window).resize(function () {
                $ifram.height(getIframeHeight());
            });

            $pageIframeWrapper.show();
        } else {
            //显示开发环境页面
            var getStr = function (target) {
                return target || ''
            };

            var getHtml = function (nodes) {
                var html = '';
                if (nodes) {
                    var node;
                    for (var i = 0; node = nodes[i]; i++) {
                        if (showNode(node)) {
                            html += '<tr height="34">\
                            <td><p class="name padding_' + node.level + '">' + node.name + '</p></td>\
                            <td><p>' + getStr(node.wxno) + '</p></td>\
                            <td><p>' + getStr(node.apiurl) + '</p></td>\
                            <td><p>' + getStr(node.token) + '</p></td>\
                            <td><p>'
                            if (node.token) {
                                html += '<a href="/page/home/home.shtml?token=' + node.token + '">全民营销</a>'
                            }
                            html += '</p></td>\
                            </tr>';
                        }

                        html += getHtml(node.nodes);
                    }
                }
                return html;
            };

            function showNode(node) {
                if (node.token)return true;
                else {
                    if (node.nodes && node.nodes.length > 0) {
                        for (var i = 0; i < node.nodes.length; i++) {
                            if (showNode(node.nodes[i])) {
                                return true;
                            }
                        }
                        return false;
                    }
                    else {
                        return false;
                    }
                }
            }

            util.myAjax({
                url: G_CONFIG.api_prefix + "config/bu-token/list",
                success: function (data) {
                    if (data.retCode == "0") {
                        $('#list').html(getHtml(data.data));
                    } else {
                        util.goToLoginPage();
                    }
                }
            });
            $("#page_for_dev").show();
        }
    });


});